//
//  HttpRequest.m
//  FuZhu
//
//  Created by app on 2017/9/14.
//  Copyright © 2017年 王文. All rights reserved.
//

#import "HttpRequest.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+MJ.h"

@implementation HttpRequest
+(void)getInfo:(NSString *)info successBlock:(SuccessBlock)success failedBlock:(FailedBlock)fail {
    NSString *urlStr = [NSString stringWithFormat:@"url"];
    [self getWithURL:urlStr successBlock:success failedBlock:fail];
}
+(void)PostInfo:(NSDictionary *)info successBlock:(SuccessBlock)success failedBlock:(FailedBlock)fail {
    NSString *urlStr = [NSString stringWithFormat:@"url"];
    [self PostWithParameter:info url:urlStr successBlock:success failedBlock:fail];
}
+(void)getWithURL:(NSString *)url successBlock:(SuccessBlock)success failedBlock:(FailedBlock)fail {
    NSLog(@"request url = %@", url);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20.0f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
        [MBProgressHUD showError:@"请检查你的网络"];
    }];
}
+(void)PostWithParameter:(NSDictionary *)paras url:(NSString *)url successBlock:(SuccessBlock)success failedBlock:(FailedBlock)fail {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    // 设置相应内容类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    // 设置超时时间
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20.0f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    [manager POST:url parameters:paras progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
        [MBProgressHUD showError:@"请检查你的网络"];
    }];
}
+(void)uploadImage:(UIImage *)image paras:(NSDictionary *)paras successBlock:(SuccessBlock)success failedBlock:(FailedBlock)fail {
    
}
+(void)uploadImages:(NSArray *)imageArr paras:(NSDictionary *)paras successBlock:(SuccessBlock)success failedBlock:(FailedBlock)fail {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    // 设置相应内容类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    // 设置超时时间
    [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
    manager.requestSerializer.timeoutInterval = 20.0f;
    [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud  = [MBProgressHUD showHUDAddedTo:keyWindow animated:YES];
    hud.mode            = MBProgressHUDModeAnnularDeterminate;
    hud.labelText       = @"正在上传";
    [manager POST:@"url" parameters:paras constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        // 上传 多张图片
        ICLog(@"picArray:%@",imageArr);
        if (imageArr.count) {
            int i = 0;
            //根据当前系统时间生成图片名称
            NSDate *date = [NSDate date];
            NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
            [formatter setDateFormat:@"yyyy年MM月dd日"];
            NSString *dateString = [formatter stringFromDate:date];
            for (UIImage *image in imageArr) {
                NSString *fileName = [NSString stringWithFormat:@"%@%d.png",dateString,i];
                NSData * imageData = UIImageJPEGRepresentation(image, 0.5);
                NSLog(@"图片大小:%ld K" , (long)imageData.length / 1024);
                [formData appendPartWithFileData:imageData name:@"images[]" fileName:fileName mimeType:@"image/jpg/png/jpeg"];
            }
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        // 这里可以获取到目前的数据请求的进度
        hud.progress    = uploadProgress.fractionCompleted;
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [hud hide:YES];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        success(dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
        hud.labelText   = @"上传失败";
        [hud hide:YES];
    }];
}
@end
